package com.zxl.designpattern.behavioral.interpreter;

/**
 * 解释器模式测试类，也是客户端角色
 */
public class Test {
    public static void main(String[] args) {
        // 创建上下文
        Context context = new Context();
        context.assign("a", 5); // a = 5
        context.assign("b", 3); // b = 3
        context.assign("c", 2); // c = 2
        
        // 构建表达式: a + b - c
        IExpression expression = new SubtractExpression(
            new AddExpression(
                new VariableExpression("a"),
                new VariableExpression("b")
            ),
            new VariableExpression("c")
        );
        
        // 解释并计算表达式
        int result = expression.interpret(context);
        System.out.println("表达式 a + b - c 的结果是: " + result);
        
        // 构建更复杂的表达式: (a + b) * (b - c)
        IExpression complexExpression = new MultiplyExpression(
            new AddExpression(
                new VariableExpression("a"),
                new VariableExpression("b")
            ),
            new SubtractExpression(
                new VariableExpression("b"),
                new VariableExpression("c")
            )
        );
        
        // 解释并计算复杂表达式
        int complexResult = complexExpression.interpret(context);
        System.out.println("表达式 (a + b) * (b - c) 的结果是: " + complexResult);
    }
}    